package com.zzx.test.thread.juc;

import java.util.concurrent.TimeUnit;
import java.util.concurrent.locks.ReentrantLock;

/**
 * @ClassName: ReentrantLockTest2
 * @Description: 可重入锁测试类2
 * @Author ZhouZhiXin
 * @Date 2022/3/3
 * @Version 1.0
 */
public class ReentrantLockTest2 {

    public static void main(String[] args) throws InterruptedException {
        LockTest2 lockTest2 = new LockTest2();
        new Thread(() -> {
            lockTest2.lockTest1();
        }, "A").start();
        TimeUnit.MILLISECONDS.sleep(100);
        new Thread(() -> {
            lockTest2.lockTest2();
        }, "B").start();
    }

}

class LockTest2 {

    ReentrantLock lock = new ReentrantLock(true);

    public synchronized void lockTest1() {
        try {
            System.out.println(Thread.currentThread().getName() + " in");
            TimeUnit.MILLISECONDS.sleep(500);
            System.out.println(Thread.currentThread().getName() + " out");
            TimeUnit.MILLISECONDS.sleep(500);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public synchronized void lockTest2() {
        System.out.println("lockTest2");
    }

}
